{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___\n",
    "\n",
    "<a href='https://www.udemy.com/user/joseportilla/'><img src='../Pierian_Data_Logo.png'/></a>\n",
    "___\n",
    "<center><em>Content Copyright by Pierian Data</em></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Errors and Exceptions Homework - Solution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1\n",
    "Handle the exception thrown by the code below by using <code>try</code> and <code>except</code> blocks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An error occurred!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    for i in ['a','b','c']:\n",
    "        print(i**2)\n",
    "except:\n",
    "    print(\"An error occurred!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2\n",
    "Handle the exception thrown by the code below by using <code>try</code> and <code>except</code> blocks. Then use a <code>finally</code> block to print 'All Done.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Can't divide by Zero!\n",
      "All Done!\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "y = 0\n",
    "try:\n",
    "    z = x/y\n",
    "except ZeroDivisionError:\n",
    "    print(\"Can't divide by Zero!\")\n",
    "finally:\n",
    "    print('All Done!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3\n",
    "Write a function that asks for an integer and prints the square of it. Use a <code>while</code> loop with a <code>try</code>, <code>except</code>, <code>else</code> block to account for incorrect inputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ask():\n",
    "    \n",
    "    while True:\n",
    "        try:\n",
    "            n = int(input('Input an integer: '))\n",
    "        except:\n",
    "            print('An error occurred! Please try again!')\n",
    "            continue\n",
    "        else:\n",
    "            break\n",
    "            \n",
    "        \n",
    "    print('Thank you, your number squared is: ',n**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input an integer: null\n",
      "An error occurred! Please try again!\n",
      "Input an integer: 2\n",
      "Thank you, your number squared is:  4\n"
     ]
    }
   ],
   "source": [
    "ask()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Great Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
